001 /*
002 * Copyright 2003-2005 The Apache Software Foundation
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package net.dpml.cli.builder;
017
018 import java.util.HashSet;
019 import java.util.Set;
020
021 import net.dpml.cli.Argument;
022 import net.dpml.cli.Group;
023 import net.dpml.cli.option.Switch;
024 import net.dpml.cli.resource.ResourceConstants;
025 import net.dpml.cli.resource.ResourceHelper;
026
027 /**
028 * Builds Switch instance.
029 *
030 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
031 * @version 1.0.0
032 */
033 public class SwitchBuilder
034 {
035 private final String m_enabledPrefix;
036 private final String m_disabledPrefix;
037 private String m_description;
038 private String m_preferredName;
039 private Set m_aliases;
040 private boolean m_required;
041 private Argument m_argument;
042 private Group m_children;
043 private int m_id;
044 private Boolean m_switchDefault;
045
046 /**
047 * Creates a new SwitchBuilder using defaults.
048 * @see Switch#DEFAULT_ENABLED_PREFIX
049 * @see Switch#DEFAULT_DISABLED_PREFIX
050 */
051 public SwitchBuilder()
052 {
053 this( Switch.DEFAULT_ENABLED_PREFIX, Switch.DEFAULT_DISABLED_PREFIX );
054 }
055
056 /**
057 * Creates a new SwitchBuilder
058 * @param enabledPrefix the prefix to use for enabling the option
059 * @param disabledPrefix the prefix to use for disabling the option
060 * @throws IllegalArgumentException if either prefix is less than 1
061 * character long or the prefixes match
062 */
063 public SwitchBuilder( final String enabledPrefix, final String disabledPrefix )
064 throws IllegalArgumentException
065 {
066 if( ( enabledPrefix == null ) || ( enabledPrefix.length() < 1 ) )
067 {
068 throw new IllegalArgumentException(
069 ResourceHelper.getResourceHelper().getMessage(
070 ResourceConstants.SWITCH_ILLEGAL_ENABLED_PREFIX ) );
071 }
072
073 if( ( disabledPrefix == null ) || ( disabledPrefix.length() < 1 ) )
074 {
075 throw new IllegalArgumentException(
076 ResourceHelper.getResourceHelper().getMessage(
077 ResourceConstants.SWITCH_ILLEGAL_DISABLED_PREFIX ) );
078 }
079
080 if( enabledPrefix.equals( disabledPrefix ) )
081 {
082 throw new IllegalArgumentException(
083 ResourceHelper.getResourceHelper().getMessage(
084 ResourceConstants.SWITCH_IDENTICAL_PREFIXES ) );
085 }
086
087 m_enabledPrefix = enabledPrefix;
088 m_disabledPrefix = disabledPrefix;
089 reset();
090 }
091
092 /**
093 * Creates a new Switch instance
094 * @return a new Switch instance
095 */
096 public Switch create()
097 {
098 final Switch option =
099 new Switch(
100 m_enabledPrefix,
101 m_disabledPrefix,
102 m_preferredName,
103 m_aliases,
104 m_description,
105 m_required,
106 m_argument,
107 m_children,
108 m_id,
109 m_switchDefault );
110 reset();
111 return option;
112 }
113
114 /**
115 * Resets the builder.
116 * @return the builder
117 */
118 public SwitchBuilder reset()
119 {
120 m_description = null;
121 m_preferredName = null;
122 m_required = false;
123 m_aliases = new HashSet();
124 m_argument = null;
125 m_children = null;
126 m_id = 0;
127 m_switchDefault = null;
128 return this;
129 }
130
131 /**
132 * Use this option description
133 * @param newDescription the description to use
134 * @return this builder
135 */
136 public SwitchBuilder withDescription( final String newDescription )
137 {
138 m_description = newDescription;
139 return this;
140 }
141
142 /**
143 * Use this option name. The first name is used as the preferred
144 * display name for the Command and then later names are used as aliases.
145 *
146 * @param name the name to use
147 * @return this builder
148 */
149 public SwitchBuilder withName( final String name )
150 {
151 if( m_preferredName == null )
152 {
153 m_preferredName = name;
154 }
155 else
156 {
157 m_aliases.add( name );
158 }
159 return this;
160 }
161
162 /**
163 * Use this optionality
164 * @param newRequired true iff the Option is required
165 * @return this builder
166 */
167 public SwitchBuilder withRequired( final boolean newRequired )
168 {
169 m_required = newRequired;
170 return this;
171 }
172
173 /**
174 * Use this Argument
175 * @param newArgument the argument to use
176 * @return this builder
177 */
178 public SwitchBuilder withArgument( final Argument newArgument )
179 {
180 m_argument = newArgument;
181 return this;
182 }
183
184 /**
185 * Use this child Group
186 * @param newChildren the child Group to use
187 * @return this builder
188 */
189 public SwitchBuilder withChildren( final Group newChildren )
190 {
191 m_children = newChildren;
192 return this;
193 }
194
195 /**
196 * Sets the id
197 *
198 * @param newId the id of the Switch
199 * @return this SwitchBuilder
200 */
201 public final SwitchBuilder withId( final int newId )
202 {
203 m_id = newId;
204 return this;
205 }
206
207 /**
208 * Sets the default state for this switch
209 *
210 * @param newSwitchDefault the default state
211 * @return this SwitchBuilder
212 */
213 public final SwitchBuilder withSwitchDefault( final Boolean newSwitchDefault )
214 {
215 m_switchDefault = newSwitchDefault;
216 return this;
217 }
218 }